import api from "/api.js";
import network from '/network.js';

class Wechat {
  /** 
   * 登陆 
   * @return {Promise}  
   */
  static login() {
    return new Promise((resolve, reject) => wx.login({
      success: resolve,
      fail: reject
    }));
  }

  /** 
   * 获取用户信息 
   * @return {Promise}  
   */
  static getUserInfo() {
    return new Promise((resolve, reject) => wx.getUserInfo({
      success: resolve,
      fail: reject
    }));
  }

  /** 
   * 获取用户信息 
   * @return {Promise}  
   */
  static getSetting() {
    return new Promise((resolve, reject) => wx.getSetting({
      success: resolve,
      fail: reject
    }));
  }

  /** 
   * 解析微信上手机号 
   * @return {Promise}  
   */
  static getPhoneNumber(that, e, id, isBack = false) {
    let _this = this;
    wx.checkSession({
      success() {
        //session_key 未过期，并且在本生命周期一直有效
        _this.decodedPhone(that, e, id, getApp().globalData.sessionCode, isBack);
      },
      fail() {
        // session_key 已经失效，需要重新执行登录流程
        wx.login({
          success(res) {
            if (res.code) {
              _this.decodedPhone(that, e, id, res.code, isBack);
            }
          }
        })
      }
    })



    //   this.login().then(res => {
    //     return wx.cloud.callFunction({
    //       name: 'session',
    //       data: {
    //         session_code: res.code
    //       }
    //     })
    //   }).then(res => {
    //     return network.postJson(that, api.decodePhone, {
    //       encryptedData: e.detail.encryptedData,
    //       iv: e.detail.iv,
    //       sessionKey: res.result.session_key,
    //       openId: res.result.openid
    //     })
    //   }).then(res => {
    //     console.log(res)
    //     let phone = res.data.data;
    //     // avatar - 头像
    //     // phone - 手机号
    //     // name - 姓名
    //     return network.postJson(that, api.saveUser, {
    //       memberId: id,
    //       phone: phone
    //     })
    //   }).then(res => {
    //     if (!res.data.data.isPartner) {
    //       res.data.data.isPartner = 0;
    //     }
    //     if (!res.data.data.account_number) {
    //       res.data.data.account_number = "";
    //     } else {
    //       let phone = res.data.data.account_number;
    //       that.setData({
    //         phone: phone.substring(0, 3) + "****" + phone.substring(7, 11)
    //       })
    //     }
    //     if (!res.data.data.shoppingcart_data) {
    //       res.data.data.shoppingcart_data = {
    //         data: [],
    //         total: 0
    //       };
    //     } else {
    //       if (res.data.data.shoppingcart_data.total > 0) {
    //         wx.setTabBarBadge({
    //           index: 1,
    //           text: res.data.data.shoppingcart_data.total.toString()
    //         })
    //       }
    //     }
    //     that.setData({
    //       userInfo: res.data.data
    //     })

    //     getApp().globalData.userInfo = res.data.data;
    //     that.toast.showToast("手机号获取成功");
    //   })
  }

  /** 
   * 解析微信上手机号
   * @return {Promise}  
   */
  static decodedPhone(that, e, id, code, isBack = false) {
    console.log(e)
    console.log(code)
    wx.cloud.callFunction({
      name: 'session',
      data: {
        session_code: code,
        encryptedData: e.detail.encryptedData,
        iv: e.detail.iv
      }
    }).then(res => {
      console.log(res)
      return network.postJson(that, api.saveUser, {
        memberId: id,
        phone: res.result.phoneNumber
      })
    }).then(res => {
      if (!res.data.data.isPartner) {
        res.data.data.isPartner = 0;
      }
      if (!res.data.data.account_number) {
        res.data.data.account_number = "";
      } else {
        let phone = res.data.data.account_number;
        that.setData({
          phone: phone.substring(0, 3) + "****" + phone.substring(7, 11)
        })
      }
      if (!res.data.data.shoppingcart_data) {
        res.data.data.shoppingcart_data = {
          data: [],
          total: 0
        };
      } else {
        if (res.data.data.shoppingcart_data.total > 0) {
          wx.setTabBarBadge({
            index: 1,
            text: res.data.data.shoppingcart_data.total.toString()
          })
        }
      }
      that.setData({
        userInfo: res.data.data
      })

      if (isBack) {
        wx.navigateBack()
      }

      getApp().globalData.userInfo = res.data.data;
      that.toast.showToast("手机号获取成功");
    })
  }

  /** 
   * 读取app.js用户信息 
   * 此方法主要是为了避免重复写回调来读取已从服务器上获取到的用户信息
   */
  static getAppUser(app, fun) {
    if (app.globalData.userInfo) {
      fun(app.globalData.userInfo);
    } else {
      app.userInfoCallback = res => {
        fun(res);
      }
    }
  }
}

module.exports = Wechat;